/* 
 * File:   repository_tree_view.cpp
 * Author: jolo
 * 
 * Created on: 11. Oktober 2011, 12:07
 * Lincense: New BSD License (The BSD 2-Clause License)
 */

#include "repository_tree_view.hpp"

#include <widgets/entity_item.hpp>
#include <widgets/tree_view_context_menu.hpp>

#include "repository_tree_model.hpp"
#include "main_window.hpp"

#include <QtCore/QModelIndex>
#include <QtGui/QMenu>
#include <QtGui/QAction>
#include <QtGui/QStandardItem>

#include <model/repository.hpp>
#include <model/project.hpp>
#include <model/chemical_element.hpp>

namespace widgets {
    class tree_view_context_menu;
}

namespace gui
{
    repository_tree_view::repository_tree_view(QWidget* p)
    : widgets::tree_view(p)
    {
        setRootIsDecorated(true);
        setAllColumnsShowFocus(true);
        setContextMenuPolicy(Qt::CustomContextMenu);

        resize_columns_to_content();
        connect(this, SIGNAL(activated(const QModelIndex&)),
                this, SLOT(on_model_index_activated(const QModelIndex&)));
        connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
                this, SLOT(on_context_menu_request(const QPoint&)));
    }

    repository_tree_view::~repository_tree_view()
    {
    }

    void repository_tree_view::setModel(repository_tree_model* m)
    {
        m_ = m;
        tree_view::setModel(m);
        connect(model(), SIGNAL(reloaded()),
                this, SLOT(resize_columns_to_content()));

        resize_columns_to_content();
    }

    void repository_tree_view::resize_columns_to_content()
    {
        if (!model())
        {
            return;
        }

        for (int i = 0; i != model()->columnCount(); ++i)
        {
            resizeColumnToContents(i);
        }
    }

    void repository_tree_view::on_context_menu_request(const QPoint& pos)
    {
        QModelIndex idx = indexAt(pos);

        if (!idx.isValid())
        {
            return;
        }

        QStandardItem * itm(dynamic_cast<widgets::entity_item*> (m_->itemFromIndex(idx)));

        if (!itm)
        {
            return;
        }

        handle_context_menu_for_item(dynamic_cast<widgets::entity_item*> (itm), pos);
    }

    void repository_tree_view::handle_context_menu_for_item(widgets::entity_item* i, const QPoint& pos)
    {
        widgets::tree_view_context_menu* m = i->create_context_menu(this, m_);
        connect(m, SIGNAL(create_request(widgets::entity_item*)), 
                m_, SLOT(on_create_request(widgets::entity_item*)));
        connect(m, SIGNAL(edit_request(widgets::entity_item*)),
                m_, SLOT(on_edit_request(widgets::entity_item*)));
        connect(m, SIGNAL(delete_request(widgets::entity_item*)),
                m_, SLOT(on_delete_request(widgets::entity_item*)));
        m->exec(mapToGlobal(pos));

    }

    void repository_tree_view::on_model_index_activated(const QModelIndex& idx)
    {
        if (!idx.isValid())
        {
            emit selected(model::entity_ptr());
            return;
        }

        QStandardItem* itm = (dynamic_cast<QStandardItemModel*> (model()))->itemFromIndex(idx);

        if (!itm)
        {
            emit selected(model::entity_ptr());
            return;
        }

        widgets::entity_item* i = dynamic_cast<widgets::entity_item*> (itm);

        if (i)
        {
            emit selected(i->entity());
            return;
        }
        emit selected(model::entity_ptr());
    }
}
